// This file is part of Visuals, and is based on Winamp's AVS plugin. 
// Copyright 2005 Nullsoft, Inc.
// Copyright 2011 Scott Sibley

var n, b, x, y, i, v, t, d, w, h, red, green, blue, linesize, skip, needs_init;

function r_sscope(name, init, frame, beat, point) {
	this.name = name;
	this.variables = new Array();
	//this.type = TYPE_SSCOPE;
	this.init = init;
	this.frame = frame;
	this.beat = beat;
	this.point = point;
	this.addvar("n", 1);
	this.addvar("b", 0);
	this.addvar("x", 0);
	this.addvar("y", 0);
	this.addvar("i", 0);
	this.addvar("v", 0);
	this.addvar("t", 0);
	this.addvar("d", 0);
	this.addvar("w", width);
	this.addvar("h", height);
	this.addvar("red", 0);
	this.addvar("green", 0);
	this.addvar("blue", 0);
	this.addvar("linesize", 0);
	this.addvar("skip", 0);
	this.addvar("bgcolor", bgcolor);
	this.addvar("fgcolor", fgcolor);
	this.addvar("needs_init", true);
}

function sscope_render(this_thread, max_threads, visdata, isBeat, buffer, fbout) {
	var size = 5;

	eval(this.getenv());

	if(needs_init) {
		needs_init = false;
		this.init();
		this.beat();
	}

	var b = isBeat;
	var a, l;
	var size = width;
	var lx = 0, ly = 0, yy, xx;
	var current_color = fgcolor;;
	var ws = 1;
	var xorv = (ws * size) ^ size;

	var c = Color2RGB(fgcolor);
	blue = c[0] / 255;
	green =  c[1] / 255;
	red = c[2] / 255;
	alpha = c[3] / 255;
	skip = 0;
	linesize = 3

	this.frame();

	if (b) {
		this.beat();
	}


	l = n;

	if (l > size*size) {
		l = size*size;
	}



	if( l == 0 || l == 1 ) {
		l = 2;
	}


	for(a = 0; a < l; a++) {
		v = visdata[a % visdata.length];
		i = a/(l-1);
		skip = 0;

		this.point();

		var xx = Math.floor((x + 1.0) * width * 0.5);
		var yy = Math.floor((y + 1.0) * height * 0.5);
		var this_color = RGB2Color(red * 255, green * 255, blue * 255, 0xff);

		if (skip == 0) {
			if (drawMode == 0) {
				if (yy >= 0 && yy < height && xx >= 0 && xx < width) {
					BLEND.blend(line_blend_mode, buffer, (yy * width + xx) * pixel * pixel, this_color);
				}
			} else {
				if (a > 0) {
					if (yy >= 0 && yy < h && xx >= 0 && xx < w &&
						ly >= 0 && ly < h && lx >= 0 && lx < w) {
								if(!redraw)
									buffer_line(buffer, lx * pixel * xscale, ly * pixel * yscale, xx * pixel * xscale, yy * pixel * yscale, this_color, width * pixel);
								else
									buffer_line(buffer, lx, ly, xx, yy, this_color, width);			
							
					}
				}
			}
			lx = xx;
			ly = yy;
		}
	}

	eval(this.setenv());

	return 0;

}

r_sscope.prototype.render = sscope_render;
r_sscope.prototype.addvar = addvar;
r_sscope.prototype.getvar = getvar;
r_sscope.prototype.setvar = setvar;
r_sscope.prototype.getenv = getenv;
r_sscope.prototype.setenv = setenv;
